import { ref, onMounted, onUnmounted } from 'vue'
import dayjs from "dayjs";

/*时间*/
export const useTime = () => {

  const weekArr = [
    "星期日",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六"
  ];

  const interval = ref(null)
  const currentTime = ref(dayjs().format('YYYY-MM-DD HH:mm:ss'))
  const week = weekArr[dayjs().format("d")]

  onMounted(() => setTime())
  onUnmounted(() => {
    if(interval.value){
      clearInterval(interval.value)
    }
  })

  const setTime = () => {
    if(!interval.value) {
      interval.value = setInterval(() => {
        currentTime.value = dayjs().format('YYYY-MM-DD HH:mm:ss')
      },1000)
    }
  }

  return {
    week,
    currentTime
  }
}
